package cn.xaut.剑指offer.m01;

import java.util.Arrays;
import java.util.Stack;

/**
 * 剑指 Offer 06. 从尾到头打印链表
 */
public class demo08 {

    public int[] reversePrint(ListNode head) {

        ListNode cur = head;
        Stack<Integer> stack = new Stack<>();
        while (cur != null) {
            stack.push(cur.val);
            cur = cur.next;
        }
        int size = stack.size();
        int[] res = new int[size];
        for (int i = 0; i < size; i++) 
            res[i] = stack.pop();
        
        return res;
    }

    // 递归解法
    int[] res;
    int i = 0, j = 0;

    public void recur(ListNode head) {
        if (head == null) {
            res = new int[i];
            return;
        }
        i ++;
        recur(head.next);
        res[j] = head.val;
        j ++;
    }
    public int[] reversePrint2(ListNode head) {
        recur(head);
        return res;
    }
    
    
    
    public static void main(String[] args) {

        Stack<Integer> stack = new Stack<>();
        stack.push(1);
        stack.push(3);
        stack.push(2);
        System.out.println(stack);
        int size = stack.size();
        int[] res = new int[size];
        for (int i = 0; i < size; i++) 
            res[i] = stack.pop();
        System.out.println(Arrays.toString(res));
        
    }
}
